/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.taglib.head.js.impl;

import java.util.Set;
import java.util.TreeSet;

import com.genia.toolbox.web.taglib.head.js.JsFunction;
import com.genia.toolbox.web.taglib.head.js.JsFunctionContainer;

/**
 * implementation of {@link JsFunctionContainer}.
 */
public class JsFunctionContainerImpl
    implements JsFunctionContainer
{

  /**
   * the default name of the javascript function.
   */
  private String functionsBaseName;

  /**
   * the current unique id used to generate function names.
   */
  private transient byte functionIdentifier = 0;

  /**
   * the set of <code>JsFunction</code> that must be called in the
   * initialization process.
   */
  private transient final Set<JsFunction> jsFunctions = new TreeSet<JsFunction>();



  /**
   * add a new function with its order.
   * 
   * @param function
   *          the name of the function to add
   * @param order
   *          the priority of the function to add
   */
  public void addFunction(final String function, final int order)
  {
    jsFunctions.add(new JsFunction(function, order));
  }



  /**
   * returns the set of <code>JsFunction</code> that must be called in the
   * initialization process.
   * 
   * @return the set of <code>JsFunction</code> that must be called in the
   *         initialization process
   */
  public Set<JsFunction> getFunctions()
  {
    return jsFunctions;
  }



  /**
   * returns an unique id used to generate function names.
   * 
   * @return the an unique id to use to generate function names
   */
  public short getNextFunctionIdentifier()
  {
    return functionIdentifier++;
  }



  /**
   * getter for the functionsBaseName property. This is the common part of the
   * name of the functions begin generated.
   * 
   * @return for the functionsBaseName
   * @see com.genia.toolbox.web.taglib.head.js.JsFunctionContainer#getFunctionsBaseName()
   */
  public String getFunctionsBaseName()
  {
    return functionsBaseName;
  }



  /**
   * setter for the functionsBaseName property.
   * 
   * @param functionsBaseName
   *          the functionsBaseName to set
   */
  public void setFunctionsBaseName(String functionsBaseName)
  {
    this.functionsBaseName = functionsBaseName;
  }

}
